package com.easypay.reactive8583.netty.pipeline;

import com.easypay.reactive8583.IsoMessageListener;
import com.easypay.reactive8583.iso.MessageClass;
import com.easypay.reactive8583.iso.MessageFactory;
import com.solab.iso8583.IsoMessage;
import io.netty.channel.ChannelHandlerContext;

/**
 * @author pandans
 * @date 2021/03/04 15:43
 * @Description:
 */
public class EchoMessageListener<T extends IsoMessage> implements IsoMessageListener<T> {
    private MessageFactory<T> isoMessageFactory;

    public EchoMessageListener(MessageFactory<T> isoMessageFactory) {
        this.isoMessageFactory = isoMessageFactory;
    }

    @Override
    public boolean applies(T isoMessage) {
        return isoMessage.getType() != 0 && MessageClass.NETWORK_MANAGEMENT.getValue() != 0;
    }

    @Override
    public boolean onMessage(ChannelHandlerContext ctx, T isoMessage) {
        IsoMessage echoResponse = isoMessageFactory.createResponse(isoMessage);
        ctx.writeAndFlush(echoResponse);
        return false;
    }
}
